#-------------------------------------------------------------------------------
# Name:        png_to_notes_converter.py
# Purpose:     Classes to generate empty SlideSpeech seq of empty notes
#              from list of png images in specified directory
#
# Authors:     John Graves <john.graves@slidespeech.com>
#
# Copyright:   (c) Slidespeech Ltd 2012
# Licence:     MIT license
#-------------------------------------------------------------------------------

import os, glob, sys
from converter import *

class PngToNotesConverter(Converter):
    def __init__(self, previous=None, pngDir=None):
        Converter.__init__(self, previous)
        if pngDir is None:
            self._imageDir = os.path.join(self.tempDir(),'images','CONTENTS')
        else:
            self._imageDir = pngDir

    def _convertImpl(self):

        # shouldn't be any need to check for an upper case extension
        self._slideList = sorted(glob.glob(os.path.join(self._imageDir, "*.png")))

        # add the empty notes to the script
        try:
            newSeq = {'S':[]}
            for slide in self._slideList:
                newSeq['S'].append({'q':'There are no speaker notes for this image','i':slide})
            seq = self.script()
            del seq['S']
            seq['S'] = newSeq['S']

        except KeyError as e:
            ex = ConverterError("Field missing in slide script", e)
            raise ex

    def slideList(self):
        return self._slideList

if __name__ == "__main__":
    # a simple test/demo
    c = PngToNotesConverter(previous=None, pngDir=sys.argv[1])
    c.convert()
    print(c.script())
